package cn.com.tiptop.license.controller.interceptor;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.com.tiptop.license.controller.annotation.PermessionLimit;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 权限拦截
 * @author admin
 */
public class PermissionInterceptor extends HandlerInterceptorAdapter {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		
		if (!(handler instanceof HandlerMethod)) {
			return super.preHandle(request, response, handler);
		}

		HandlerMethod method = (HandlerMethod)handler;
		PermessionLimit permission = method.getMethodAnnotation(PermessionLimit.class);
		boolean limit = (permission != null)?permission.limit():true;
		boolean superUser = (permission != null)?permission.superUser():false;
		if(!true) {
			response.sendRedirect(request.getContextPath() + "/toLogin");	
			return false;
		}
		
		return super.preHandle(request, response, handler);
	}
	
}
